﻿// JScript 文件
var o_productid;
var c_orderArray = new Array();

function outputComment(productid)
{
    o_productid = productid;  
    
    var r = outputWriteComment();   
    document.writeln(r);
    getCommentData(productid, 1);
}

function getCommentData(productid, page)
{
    try
    {
        if(c_orderArray[productid+"_" + page] != null)
        {
            printComment(c_orderArray[productid+"_" + page]);
            return;
        }
        
        var xmlHttp = XmlHttp.create();
        var requestUrl = "WebService/ShopService.ashx?Action=GetOrderList&ProductID=" + productid + "&page=" + page + "&rnd=" + Math.random();
        
        xmlHttp.open("GET", requestUrl, true);
        xmlHttp.onreadystatechange = function(){
            //try
//            {
                if(xmlHttp.readyState == 4)
                {
                   
                    var xmlDoc = xmlHttp.responseXML;
                      
                    var data = new commentData(xmlDoc, page);
                    printComment(data);
                    c_orderArray[productid+"_" + page] = data;
                }
//            }
            //catch(exception)
//            {
//                ;
//            }
//            
        };
        xmlHttp.send(null);
    }
    catch(exception)
    {
       
    }
    
}

function printComment(data)
{
    if (data.pageSize > 0) {
         document.getElementById("saleOrderList").innerHTML = data.outString();
        if(data.pageSize>1)
            outputPage(data.pageSize, data.page);
    }
    else {
      document.getElementById("saleOrderList").innerHTML = '<div style="text-align:center; height:20px; font-size:14px;">暂时没有买家购买此商品！</div>';
    }
}

function outputPage(pageCount, page)
{
    var start = ((page-4)>1?page-4:1);
    var end = ((start+9)<pageCount?start+9:pageCount);
    start = ((end-9)>1?end-9:start);
    var s = '<a '
    s += (page>1?'href="javascript:" onclick="getCommentData(o_productid,'+(page-1).toString()+');"':'disabled="disabled"');
    s += '>上一页</a>';
    for(var i=start; i<=end; i++)
    {
        s += '<span style="width: 5px;">&nbsp;</span>';
        if(i != page)
            s += '<a class="AspNetPage" title="转到第'+i+'页" href="javascript:" onclick="getCommentData(o_productid,'+i+ ');">'+i+'</a>';
        else
            s += '<font style="font-weight:Bold;color: red;">'+i+'</font>';
    }
    s += '<span style="width: 4px;">&nbsp;</span><a ';
    s += (page<pageCount?'href="javascript:" onclick="getCommentData(o_productid,'+(page+1).toString()+');"':'disabled="disabled"');
    s += '>下一页</a>';
    document.getElementById("commentsPager").innerHTML = s;
}

function commentData(xmlDoc, page)
{
    try{
        
        this.page = page;
        this.pageSize = parseInt(xmlDoc.selectSingleNode("//PageSize").text);
        this.recordCount = parseInt(xmlDoc.selectSingleNode("//RecordCount").text);  
        
        this.comments = new Array();
        if(this.pageSize > 0)
        {
            var nodes = xmlDoc.selectNodes("//Table");
            var ay = new Array();
            for(var i=0; i<nodes.length; i++)
            {
                var temp = new commentDetail(nodes[i]) 
                ay.push(temp);
            }
            this.comments = ay;
        }
    }
    catch(e)
    {
        this.pageSize = 0;
    }
}

commentData.prototype.outString = function(){
    var cs = this.comments;
    
    var loop = '<tr><td>{buyername}</td><td>{price}</td><td>{nums}</td><td>{updatetime}</td><td>{status}</td></tr>';
    //var loop = '{lastModifiedDate}{commentUserName}说：{commentBody}'
    var r = "";
    for(var i=0; i<cs.length; i++)
    {
        r += loop.replace(/\{buyername\}/ig, cs[i].buyername)
            .replace(/\{price\}/ig, cs[i].price).replace(/\{nums\}/ig, cs[i].nums).replace(/\{updatetime\}/ig, cs[i].updatetime)
            .replace(/\{status\}/ig, cs[i].status);
    }
    r = '<table class="SellHistory"><tr><th>买家</th><th>出价</th><th>购买数量</th><th>时间</th><th>状态</th></tr>'+ r + '</table>';
    return r;
};

function commentDetail(node)
{
    
    this.price = node.selectSingleNode("Price").text;
    this.nums = node.selectSingleNode("Quantity").text;   
    this.updatetime = node.selectSingleNode("UpdateTime").text;
    this.buyername = '<a href="Shop/' + node.selectSingleNode("UserID").text+ '" target="_blank">' +  node.selectSingleNode("BuyerName").text + '</a>';
    this.status = node.selectSingleNode("Status").text;
    var picUrl="";
    if (this.status == "1" || this.status == "2" || this.status == "0")
    {
        picUrl = "<img src='../Images/okhand.gif'/>";
    }
    else
    this.status=picUrl==""?this.status:picUrl;
    
   
   
}

function outputWriteComment()
{
    var s = '<div style="height:100px;height:auto!important;">'            
            + '<div id="saleOrderList"></div>'
            + '<div class="AspNetPage" id="commentsPager" style="text-align:center"></div>'
            + '</div>';
          
    return s;
}


function submitComment() {
    var actionObj = document.getElementById("h_action");
    var msgIDObj = document.getElementById("h_msgID");
    var txtValidate = document.getElementById("CheckCode");
    var txtCommentBody = document.getElementById("Message_Content");

    var shop_id = document.getElementById("h_shop_id").value;
    var saler_id = document.getElementById("h_saler_id").value;
    var saler_name = document.getElementById("h_saler_nickname").value;
    var product_id =  document.getElementById("h_product_id").value;
    
   
//ProductID=undefined&Rnd=0.33909237202416353
     
    
    if(txtValidate != null && txtCommentBody != null)
    {
        if(txtValidate.value.length != 5)
        {
            alert("没有填写验证码！");
            txtValidate.focus();
            return;
        }
        if(txtCommentBody.value == "")
        {
            alert("没有填写评论内容！");
            txtCommentBody.focus();
            return;
        }
        else if(txtCommentBody.value.length>400)
        {
            alert("当前字数为"+txtCommentBody.value.length+"填写评论内容超出字数限制！");
            txtCommentBody.focus();
            return;
        }
    
        var s = "Action=LeaveMsg&h_action=" + actionObj.value + "&msgID=" + msgIDObj.value              
              + "&product_id="+product_id+"&saler_name="+encodeURIComponent(saler_name)+"&saler_id="+saler_id+"&shop_id=0"
              + "&checkcode=" + encodeURIComponent(txtValidate.value) + "&content=" + txtCommentBody.value;
      
        
      
        var url = "WebService/ShopService.ashx";
        var xmlHttp = XmlHttp.create();
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Content-Length", s.length);
        xmlHttp.setRequestHeader("CONTENT-TYPE", "application/x-www-form-urlencoded");
        xmlHttp.onreadystatechange = function(){
            if(xmlHttp.readyState == 4)
            {
                var t = parseInt(xmlHttp.responseText);
                if(t == -1)
                {
                    alert("验证码错误！");
                    changeControlStatus(false);
                }
                else if(t == -2)
                    alert("发表留言失败");
                else if(t == -4)
                    alert("发表留言失败，有被禁用词汇！");
                else if(t > 0)
                {
                    alert("发表留言成功");
                    txtValidate.value = "";
                    txtCommentBody.value = "";
                    c_commentsArray = new Array();
                    GetProductMessage(product_id,"leaveMsg");                    
                }
            }
        };
        xmlHttp.send(s);
        
        changeControlStatus(true);
        setTimeout('changeControlStatus(false);', 15000)
    }
}

function changeControlStatus(disable) {
    var c0 = document.getElementById("btnCommitComment"), c1 = document.getElementById("CheckCode"), c2 = document.getElementById("Message_Content");
    var actionObj = document.getElementById("h_action");
    var msgIDObj = document.getElementById("h_msgID");
    var msgTitle=document.getElementById("leave_message_title");
    if(c0 != null)
        c0.disabled = disable;
    if(c1 != null)
        c1.disabled = disable;
    if(c2 != null)
        c2.disabled = disable;
    if(actionObj!=null)
        actionObj.value="";
    if(msgIDObj!=null)
        msgIDObj.value="";  
        
    if(msgTitle!=null)
       msgTitle.innerHTML="发表留言";    
    if(disable == false)
        reloadValidateImage();
}

function reloadValidateImage() {
    var img = document.getElementById("imgValidate");
    if(img != null)
    {
        img.src = "../../Passport/CheckCode.aspx?rnd=" + Math.random();
    }
}

function leaveAuctionMessage(ID, SenderName) {
    var actionObj = document.getElementById("h_action");
    var msgIDObj = document.getElementById("h_msgID");
    var msgTitleObj = document.getElementById("leave_message_title");
    var msgContentObj = document.getElementById("Message_Content");
     var msgCheckObj=document.getElementById("btn_confirm");
     if(actionObj!=null && msgIDObj!=null)
     {
        msgTitleObj.innerHTML="回复 [<span style=\"color:red\">"+SenderName+"</span>] 留言：";
        msgContentObj.focus();
        msgCheckObj.style.display="";
        actionObj.value="replay";
        msgIDObj.value=ID;
     
     }
   
}

